package com.qymerge.manage.base.service.impl;

import com.qymerge.manage.base.dao.IOrganizationalStructureDao;
import com.qymerge.manage.base.ro.OrganizationalStructureRO;
import com.qymerge.manage.base.service.IOrganizationalStructureService;
import com.qymerge.manage.core.bean.ActionResultRO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 *
 * @author Xiao
 * @version 1.0.0
 * @date 2025/4/23 13:21
 */
@Service
public class OrganizationalStructureServiceimpl implements IOrganizationalStructureService {

    @Autowired
    private IOrganizationalStructureDao organizationalStructureDao;

    /**
     * 后台管理系统-查询所有
     *
     * @return
     */
    @Override
    public List organizationalList(OrganizationalStructureRO roSearch) {
        return organizationalStructureDao.findAll(roSearch);
    }

    /**
     * 获取指定
     *
     * @param
     * @return
     */
    @Override
    public OrganizationalStructureRO getOrganizational(OrganizationalStructureRO ro) {
        return organizationalStructureDao.findOne(ro.getId());
    }

    /**
     * 后台管理系统-新增
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO saveOrganizational(OrganizationalStructureRO ro) {
        int rn = organizationalStructureDao.install(ro);
        return new ActionResultRO(rn, "保存成功", "保存失败");
    }

    /**
     * 后台管理系统-编辑菜单
     *
     * @param ro
     * @return
     */
    @Override
    public ActionResultRO editOrganizational(OrganizationalStructureRO ro) {
        int rn = organizationalStructureDao.update(ro);
        return new ActionResultRO(rn, "修改成功", "修改失败");
    }

    /**
     * 后台管理系统-删除
     *
     * @param
     * @return
     */
    @Override
    public ActionResultRO delOrganizational(Long id) {
        String msg = "";
        String errMsg = "";
        int rn = 0;

        //删除子菜单
        List<OrganizationalStructureRO> secondAll = organizationalStructureDao.findAllByParentId(id);
        if (CollectionUtils.isNotEmpty(secondAll)) {
            for (OrganizationalStructureRO second : secondAll) {
                long secondParentId = second.getId();
                //三级菜单
                List<OrganizationalStructureRO> thirdAll = organizationalStructureDao.findAllByParentId(secondParentId);
                if (CollectionUtils.isNotEmpty(thirdAll)) {
                    for (OrganizationalStructureRO third : thirdAll) {
                        long thirdParentId = third.getId();
                    }
                }
                organizationalStructureDao.remove(secondParentId);
            }
            organizationalStructureDao.remove(id);
        }
        //删除本菜单
        rn = organizationalStructureDao.remove(id);
        msg = "删除成功";
        errMsg = "删除失败";

        return new ActionResultRO(rn, msg, errMsg);
    }


}
